package com.tjetc.service.impl;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.*;
import com.tjetc.entity.*;
import com.tjetc.service.FoodRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.time.LocalDateTime;


@Service
public class FoodRecordServiceImpl implements FoodRecordService {
    @Autowired
    private FoodRecordMapper foodRecordMapper;
    @Autowired
    private FoodContentMapper foodContentMapper;
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Autowired
    private CaregiverInfoMapper caregiverInfoMapper;
    @Override
    public JsonResult findById(Long id) {
        FoodRecord foodRecord=foodRecordMapper.selectById(id);
        ElderInfo elderInfo=elderInfoMapper.selectById(foodRecord.getElderlyId());
        FoodContent foodContent=foodContentMapper.selectById(foodRecord.getMealId());
        CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(foodRecord.getCaregiverId());
        foodRecord.setCaregiver(caregiverInfo);
        foodRecord.setFoodContent(foodContent);
        foodRecord.setElder(elderInfo);
        return JsonResult.success(foodRecord);
    }
    @Override
    public   JsonResult findElderPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<FoodRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<FoodRecord>foodRecordPage= foodRecordMapper.selectPageLikeElderName(page,name);
        for (FoodRecord foodRecord : foodRecordPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(foodRecord.getElderlyId());
            FoodContent foodContent=foodContentMapper.selectById(foodRecord.getMealId());
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(foodRecord.getCaregiverId());
            foodRecord.setCaregiver(caregiverInfo);
            foodRecord.setFoodContent(foodContent);
            foodRecord.setElder(elderInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(foodRecordPage);
    }
    @Override
    public   JsonResult findCaregiverPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<FoodRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<FoodRecord>foodRecordPage= foodRecordMapper.selectPageLikeCaregiverName(page,name);
        for (FoodRecord foodRecord : foodRecordPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(foodRecord.getElderlyId());
            FoodContent foodContent=foodContentMapper.selectById(foodRecord.getMealId());
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(foodRecord.getCaregiverId());
            foodRecord.setCaregiver(caregiverInfo);
            foodRecord.setFoodContent(foodContent);
            foodRecord.setElder(elderInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(foodRecordPage);
    }
    @Override
    public   JsonResult findFoodCntentPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<FoodRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<FoodRecord>foodRecordPage= foodRecordMapper.selectPageLikeMealName(page,name);
        for (FoodRecord foodRecord : foodRecordPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(foodRecord.getElderlyId());
            FoodContent foodContent=foodContentMapper.selectById(foodRecord.getMealId());
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(foodRecord.getCaregiverId());
            foodRecord.setCaregiver(caregiverInfo);
            foodRecord.setFoodContent(foodContent);
            foodRecord.setElder(elderInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(foodRecordPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=foodRecordMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(FoodRecord foodRecordParam) {
        FoodRecord foodRecord=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(foodRecordParam.getRecordId()==null||foodRecordParam.getRecordId()<=0){
            //新增
            foodRecord=new FoodRecord();
            BeanUtils.copyProperties(foodRecordParam,foodRecord);
            foodRecord.setCreateTime(now);
            foodRecord.setUpdateTime(now);
            foodRecordMapper.insert(foodRecord);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            foodRecord=foodRecordMapper.selectById(foodRecordParam.getRecordId());
            if(foodRecord==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            foodRecord.setUpdateTime(now);
            foodRecord.setCaregiver(foodRecordParam.getCaregiver());
            foodRecord.setFoodContent(foodRecordParam.getFoodContent());
            foodRecord.setElder(foodRecordParam.getElder());
            foodRecord.setDate(foodRecordParam.getDate());
            foodRecord.setCaregiverId(foodRecordParam.getCaregiverId());
            foodRecord.setElderlyId(foodRecordParam.getElderlyId());
            foodRecord.setFeedback(foodRecordParam.getFeedback());
            foodRecord.setIsDeleted(foodRecordParam.getIsDeleted());
            foodRecord.setMealId(foodRecordParam.getMealId());
            foodRecord.setMealTime(foodRecordParam.getMealTime());
            foodRecord.setStatus(foodRecordParam.getStatus());
            int rows=foodRecordMapper.updateById(foodRecord);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
